package com.taa.service.impl;

import com.taa.dao.IDishInfoDao;
import com.taa.dao.IRestaurantInfoDao;
import com.taa.dao.po.DishInfo;
import com.taa.dao.po.RestaurantInfo;
import com.taa.dto.DishInfoDTO;
import com.taa.dto.DishStockDTO;
import com.taa.dto.RestaurantInfoDTO;
import com.taa.enums.ResponseCode;
import com.taa.exception.AppException;
import com.taa.service.IRestaurantService;
import jakarta.annotation.Resource;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Author: taa
 * @Desc: 商家服务实现类
 * @Create: 2025-06-22 16:40
 **/
@Service
public class RestaurantServiceImpl implements IRestaurantService {

    @Resource
    private IRestaurantInfoDao restaurantInfoDao;

    @Resource
    private IDishInfoDao dishInfoDao;

    @Override
    public List<RestaurantInfoDTO> getRestaurantList() {
        // 1. 调用DAO查询所有商家PO对象
        List<RestaurantInfo> restaurantInfos = restaurantInfoDao.findAll();
        if (restaurantInfos == null || restaurantInfos.isEmpty()) {
            return Collections.emptyList();
        }

        // 2. 将PO列表转换为DTO列表
        return restaurantInfos.stream().map(po -> {
            RestaurantInfoDTO dto = new RestaurantInfoDTO();
            BeanUtils.copyProperties(po, dto);
            return dto;
        }).collect(Collectors.toList());
    }

    @Override
    public List<DishInfoDTO> getDishesByRestaurantId(Long restaurantId) {
        // 1. 校验商家是否存在
        RestaurantInfo restaurant = restaurantInfoDao.findById(restaurantId);
        if (restaurant == null) {
            throw new AppException(ResponseCode.RESTAURANT_NOT_FOUND.getCode(), ResponseCode.RESTAURANT_NOT_FOUND.getInfo());
        }

        // 2. 调用DAO查询该商家的所有菜品PO对象
        List<DishInfo> dishInfos = dishInfoDao.findByRestaurantId(restaurantId);
        if (dishInfos == null || dishInfos.isEmpty()) {
            return Collections.emptyList();
        }

        // 3. 将PO列表转换为DTO列表
        return dishInfos.stream().map(po -> {
            DishInfoDTO dto = new DishInfoDTO();
            BeanUtils.copyProperties(po, dto);
            return dto;
        }).collect(Collectors.toList());
    }

    @Override
    public List<DishInfo> getDishListByIds(List<Long> dishIds) {
        if (dishIds == null || dishIds.isEmpty()) {
            return Collections.emptyList();
        }
        return dishInfoDao.findByIds(dishIds);
    }
    
    @Override
    @Transactional
    public void decreaseStock(List<DishStockDTO> dishStockList) {
        if (dishStockList == null || dishStockList.isEmpty()) {
            return;
        }
        for (DishStockDTO dto : dishStockList) {
            int result = dishInfoDao.decreaseStock(dto.getDishId(), dto.getQuantity());
            if (result <= 0) {
                throw new AppException(ResponseCode.STOCK_NOT_ENOUGH.getCode(), "商品ID: " + dto.getDishId() + " 库存不足");
            }
        }
    }
    
}

